﻿using UnityEngine;
using System.Collections;
using System;


public class NotificationsManager : MonoBehaviour {

	static int time1 = 86400; //24h 
	//static int time2 = 172800; //24h after previous (2 days)
	//static int time3 = 259200; //24h after previous (3 days)
	//static int time4 = 345600; //24h after previous (4 days)
	//static int time5 = 432000; //24h after previous (5 days)

	static string notificationText1 = "Come and beat your best spinning score!"; 						// na 24h (1. dan)						//11223310
	//static string notificationText2 = "Come and write down your dreams, they may come true!"; // 24h nakon prethodne (2. dan)					//11223311
	//static string notificationText3 = "Let your friends fill in the Q&A diary and get to know them!"; // 24h nakon prethodne (3. dan)			//11223312
	//static string notificationText4 = "Something fun happened to you today? Write it in your diary!"; // 24h nakon prethodne (4. dan)				//11223313
	//static string notificationText5 = "Do you know your friends well? Let them answer the questions and see!"; // 24h nakon prethodne (5. dan)	//11223314
	//public static string notificationText6 = "Come back and play Love Meter again!"; // kada se zavrsi cekanje za LoveCalculator					//11223315
	//public static string notificationText7 = "Come back and play Zodiac Match again!"; // kada se zavrsi cekanje za ZodiacMatch					//11223316

	void Awake()
	{
		#if UNITY_IOS && !UNITY_EDITOR
		// Clear all received notifications
		if (PlayerPrefs.HasKey ("LastTime"))
        NotificationServices.ClearLocalNotifications ();
		#endif
        if (!PlayerPrefs.HasKey ("LastTime")) {
            // App started for first time
            PlayerPrefs.SetString("LastTime",DateTime.Now.ToString());
            
            #if UNITY_IOS && !UNITY_EDITOR
            
            NotificationServices.RegisterForNotifications(NotificationType.Alert | NotificationType.Badge | NotificationType.Sound);
            
            #endif
            
            
            SetNottifications();
        } else {
            CancelNotiffications();
            SetNottifications();
            
        }

		CancelNotiffications();
	}

	public static void SetNottifications()
	{
		#if UNITY_ANDROID && !UNITY_EDITOR
		Debug.Log("OPALIM ti NOTIFIKACIJU");
		using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
		{
		using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
		{
		obj_Activity.Call("SendNotification",time1.ToString(),notificationText1,11223310);
		//obj_Activity.Call("SendNotification",time2.ToString(),notificationText2,11223311);
		//obj_Activity.Call("SendNotification",time3.ToString(),notificationText3,11223312);
		//obj_Activity.Call("SendNotification",time4.ToString(),notificationText4,11223313);
		//obj_Activity.Call("SendNotification",time5.ToString(),notificationText5,11223314);
		}
		}
		#endif

		#if UNITY_IOS && !UNITY_EDITOR

		LocalNotification notification1 = new LocalNotification ();
		notification1.fireDate = System.DateTime.Now.AddSeconds(/*"86400"*/86400f);
		notification1.alertAction = "Fidget Spinner";
		notification1.alertBody = "Your spinners are waiting for you!";
		notification1.hasAction = false;
		notification1.applicationIconBadgeNumber=1;
		NotificationServices.ScheduleLocalNotification (notification1);

//		LocalNotification notification2 = new LocalNotification ();
//		notification2.fireDate = System.DateTime.Now.AddSeconds(/*"259200"*/172800f);
//		notification2.alertAction = "Kitty Dress Up";
//		notification2.alertBody = "Your pet is waiting for you!";
//		notification2.hasAction = false;
//		notification2.applicationIconBadgeNumber=2;
//		NotificationServices.ScheduleLocalNotification (notification2);
//
//		LocalNotification notification3 = new LocalNotification ();
//		notification3.fireDate = System.DateTime.Now.AddSeconds(/*"432000"*/345600f);
//		notification3.alertAction = "Kitty Dress Up";
//		notification3.alertBody = "Your pet is waiting for you!";
//		notification3.hasAction = false;
//		notification3.applicationIconBadgeNumber=3;
//		NotificationServices.ScheduleLocalNotification (notification3);

		//		LocalNotification notification4 = new LocalNotification ();
		//		notification4.fireDate = System.DateTime.Now.AddSeconds(/*"604800"*/25200f);
		//		notification4.alertAction = "Game name";
		//		notification4.alertBody = "Notification text 4!";
		//		notification4.hasAction = false;
		//		notification4.applicationIconBadgeNumber=4;
		//		NotificationServices.ScheduleLocalNotification (notification4);
		#endif
	}

	public static void CancelNotiffications()
	{
		#if UNITY_ANDROID  && !UNITY_EDITOR
		Debug.Log("TREBA DA CANCELUJE NOTIFIKACIJE");
		using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
		{
		using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
		{
		obj_Activity.Call("CancelNotification",11223310);
		//obj_Activity.Call("CancelNotification",11223311);
		//obj_Activity.Call("CancelNotification",11223312);
		//obj_Activity.Call("CancelNotification",11223313);
		//obj_Activity.Call("CancelNotification",11223314);
		}
		}
		#endif

		#if UNITY_IOS && !UNITY_EDITOR
		NotificationServices.CancelAllLocalNotifications();
		#endif
	}

	public static void SetNottification(int time, string message, int ID)
	{
		#if UNITY_ANDROID && !UNITY_EDITOR
		using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
		{
		using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
		{
		obj_Activity.Call("SendNotification",time.ToString(),message,ID);
		}
		}
		#endif
	}

	public static void CancelNotiffication(int ID)
	{
		#if UNITY_ANDROID  && !UNITY_EDITOR
		Debug.Log("TREBA DA CANCELUJE NOTIFIKACIJU sa ID: " + ID);
		using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
		{
		using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
		{
		obj_Activity.Call("CancelNotification",ID);
		}
		}
		#endif
	}

	void OnApplicationPause(bool status)
	{
		if(status) // game paused
		{
			SetNottifications();
		}
		else // game resumed
		{
			CancelNotiffications();
		}
	}

	void OnApplicationQuit()
	{
		SetNottifications();
	}
}
